// pages/pointsRecords/pointsRecords.js
Page({
    data: {
      statusBarHeight: 0,
      activeTab: 0,         // 当前选中的筛选标签
      records: [],          // 积分记录列表
      loading: false,       // 是否正在加载
      hasMore: true,        // 是否还有更多数据
      pageSize: 10,         // 每页显示数量
      pageNum: 1,           // 当前页码
      filters: {            // 筛选条件
        type: '',           // 类型：''=全部，1=收入，2=支出
        startTime: '',      // 开始时间
        endTime: ''         // 结束时间
      }
    },
  
    onLoad() {
      // 获取状态栏高度
      const systemInfo = wx.getSystemInfoSync();
      this.setData({
        statusBarHeight: systemInfo.statusBarHeight
      });
      
      // 加载积分记录
      this.loadPointsRecords();
    },
  
    // 返回上一页
    goBack() {
      wx.navigateBack();
    },
  
    // 切换筛选标签
    onTabChange(event) {
        // console.log(event);
      const type = event.detail.index;
      this.setData({
        activeTab: type,
        records: [],
        pageNum: 1,
        hasMore: true
      });
      
      // 根据标签更新筛选条件
      let filterType = '';
      if (type === 1) filterType = 1;  // 收入
      if (type === 2) filterType = 2;  // 支出
      if (type === 3) filterType = 3;  // 支出
      
      this.setData({
        'filters.type': filterType
      });
      
      // 重新加载数据
      this.loadPointsRecords();
    },
  
    // 加载积分记录
    loadPointsRecords() {
      if (this.data.loading || !this.data.hasMore) return;
      
      this.setData({ loading: true });
      
      // 调用云函数获取积分记录
      wx.cloud.callFunction({
        name: 'getPointsRecords',
        data: {
          userId: wx.getStorageSync('userInfo')._id,
          pageSize: this.data.pageSize,
          pageNum: this.data.pageNum,
          type: this.data.filters.type
        },
        success: (res) => {
            console.log(res);
          if (res.result.success) {
            const newRecords = res.result.records || [];
            const hasMore = newRecords.length >= this.data.pageSize;
            if(newRecords.length){
                newRecords.forEach(item=>{
                    item.time = this.formatTime(item.createTime)
                })
            }
            this.setData({
              records: this.data.records.concat(newRecords),
              pageNum: this.data.pageNum + 1,
              hasMore: hasMore
            });
          } else {
            wx.showToast({
              title: '获取记录失败',
              icon: 'none'
            });
          }
        },
        fail: (err) => {
          console.error('获取积分记录失败:', err);
          wx.showToast({
            title: '获取记录失败',
            icon: 'none'
          });
        },
        complete: () => {
          this.setData({ loading: false });
        }
      });
    },
  
    // 下拉刷新
    onPullDownRefresh() {
      this.setData({
        records: [],
        pageNum: 1,
        hasMore: true
      });
      
      this.loadPointsRecords();
      wx.stopPullDownRefresh();
    },
  
    // 上拉加载更多
    onReachBottom() {
      this.loadPointsRecords();
    },
  
    // 格式化时间
    formatTime(timestamp) {
      if (!timestamp) return '';
      
      // 处理云开发服务器时间格式
      if (typeof timestamp === 'object') {
        timestamp = timestamp.$$date;
      }
      
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const day = date.getDate();
      const hour = date.getHours();
      const minute = date.getMinutes();
      
      return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`;
    }
  });